﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSMAECLCP
    {
        CLSREFCLCP RefClc = new CLSREFCLCP();
        private string codclc_clc;
        private string nomclc_clc;
        private string detalle_clc;
        private DataTable tabla;

        public string CODCLC_CLC
        {
            get { return codclc_clc; }
            set { codclc_clc = value; }
        }

        public string NOMCLC_CLC
        {
            get { return nomclc_clc; }
            set { nomclc_clc = value; }
        }

        public string DETALLE_CLC
        {
            get { return detalle_clc; }
            set { detalle_clc = value; }
        }

        public DataTable TABLA
        {
            get { return tabla; }
            set { tabla = value; }
        }

        MDOgeneral mod = new MDOgeneral();

        public void LimpiarValores()
        {
            CODCLC_CLC = "";
            NOMCLC_CLC = "";
            DETALLE_CLC = "";
        }

        public Boolean ExisteColeccion()
        {
            try
            {
                string query = "select codclc_clc, nomclc_clc, detalle_clc from maeclcp where codclc_clc='" + CODCLC_CLC + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select trim(codclc_clc) codclc_clc, trim(nomclc_clc) nomclc_clc, trim(detalle_clc) detalle_clc from maeclcp where codclc_clc='" + CODCLC_CLC + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    NOMCLC_CLC = dt.Rows[0][1].ToString();
                    DETALLE_CLC = dt.Rows[0][2].ToString();
                }

                else
                {
                    NOMCLC_CLC = "";
                    DETALLE_CLC = "";
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public int Eliminar()
        {
            try
            {
                string query = "delete from maeclcp where codclc_clc='" + CODCLC_CLC + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public int Actualizar()
        {
            try
            {
                string query = "update maeclcp set nomclc_clc='" + NOMCLC_CLC + "', detalle_clc='" + DETALLE_CLC + "' where codclc_clc='" + CODCLC_CLC + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public int Insertar()
        {
            try
            {
                string query = "insert into maeclcp (codclc_clc, nomclc_clc, detalle_clc) values ('" + CODCLC_CLC + "','" + NOMCLC_CLC + "','" + DETALLE_CLC + "')";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public DataTable Buscar()
        {
            try
            {
                string query = "select codclc_clc as Codigo, nomclc_clc as Nombre from maeclcp where nomclc_clc like '%" + NOMCLC_CLC + "%' order by nomclc_clc asc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int  Grabar()
        {
            RefClc.CODCLC_RXC = CODCLC_CLC;
            MDOgeneral.transaccion= MDOgeneral.conexion.BeginTransaction();
            try
            {
                if (ExisteColeccion())
                {
                    Actualizar();
                }
                else
                {
                    Insertar();
                }
                RefClc.Eliminar();

                for (int i = 0; i < TABLA.Rows.Count; i++)
                {
                    if (!TABLA.Rows[i][0].Equals(""))
                    {
                        if (RefClc.ExisteColor(TABLA.Rows[i][0].ToString().Substring(4,2)))
                        {
                            RefClc.CODCLC_RXC = CODCLC_CLC;
                            RefClc.CODREF_RXC = TABLA.Rows[i][0].ToString().Substring(0, 4);
                            RefClc.CODCOL_RXC = TABLA.Rows[i][0].ToString().Substring(4, 2);
                            if (TABLA.Rows[i][3].Equals(false))
                                RefClc.Insertar();
                            else
                                RefClc.EliminarRefCol();
                        }
                        else
                        {                
                            MDOgeneral.transaccion.Rollback();
                            return 0;
                        }

                    }
                }
                MDOgeneral.transaccion.Commit();
            }
            catch (Exception ex)
            {
                MDOgeneral.transaccion.Rollback();
                throw ex;
            }
            return 1;
        }

        public void TraerPrimerRegistro()
        {
            try
            {
                string query = "select codclc_clc, nomclc_clc, detalle_clc from maeclcp where ROWNUM<=1 order by codclc_clc";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    CODCLC_CLC = dt.Rows[0][0].ToString();
                    NOMCLC_CLC = dt.Rows[0][1].ToString();
                    DETALLE_CLC = dt.Rows[0][2].ToString();
                }

                else
                {
                    NOMCLC_CLC = "";
                    DETALLE_CLC = "";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CLSMAECLCP> listColecciones() {
            try {
                string query = "select trim(codclc_clc) codclc_clc,trim(nomclc_clc) nomclc_clc,trim(detalle_clc) detalle_clc from maeclcp order by codclc_clc";
                DataTable val=mod.consulta(query);
                List<CLSMAECLCP> lista=new List<CLSMAECLCP>();
                foreach (DataRow r in val.Rows) {
                    lista.Add(new CLSMAECLCP {
                    CODCLC_CLC=r["CODCLC_CLC"].ToString(),
                    NOMCLC_CLC = r["NOMCLC_CLC"].ToString(),
                    DETALLE_CLC = r["DETALLE_CLC"].ToString()
                    });
                }
                return lista;
            }
            catch (Exception ex) { throw ex; }
        }

    }

}
